﻿using System;
using System.Windows.Forms;
using DbfReader;
using Regedit;
using System.Collections.ObjectModel;
  
namespace DbfReaderGui
{
    public partial class DbfReaderGui : Form
    {
        public DbfReaderGui()
        {
            InitializeComponent();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = ".dbf Files|*.dbf";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                cbxFileName.Text = openFileDialog.FileName;
            }
            openFileDialog.Dispose();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (cbxFileName.Text.Trim().Length <= 0)
            {
                MessageBox.Show("The file is null.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            // Write the folder name  and file name to regedit. 
            RegeditReadAndWrite regeditReadAndWrite = new RegeditReadAndWrite(10);
            regeditReadAndWrite.WriteRegedit("ReadDbfFile", cbxFileName.Text.Trim());
            initializationDropFile();
            DbfFileReader dbfReader = new DbfFileReader(); ;
            dataGridView1.DataSource= dbfReader.Read(cbxFileName.Text.Trim());
        }

        private void DbfReaderGui_Load(object sender, EventArgs e)
        {
            initializationDropFile();
        }
        public void initializationDropFile()
        {
            // Read the folder name  and file name to regedit. 
            RegeditReadAndWrite regeditReadAndWrite = new RegeditReadAndWrite(10);
            Collection<string> Files = regeditReadAndWrite.ReadRegedit("ReadDbfFile");
            cbxFileName.Items.Clear();
            if (Files != null)
            {
                foreach (string item in Files)
                {
                    cbxFileName.Items.Add(item);
                }
                cbxFileName.SelectedIndex = 0;
            }
        }
    }
}
